<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 思考如何实现map和filter的手写思路
        const arr = [4,3,2,1]
    //foreach
    Array.prototype.myForeach=function(fn){
        for (let i = 0; i < this.length; i++) {
            fn(this[i],i,this)
            
        }

    }
    arr.myForeach(function(item,index,arr){
        console.log(item);
    console.log(index);
    console.log(arr);
    })
//filter
Array.prototype.myFilter=function(fn){
    const list = []
    for(let i = 0 ;i<this.length;i++){
        if(fn(this[i])){
            list.push(this[i])      
        }       
    }
    return list 
}
const demo = arr.myFilter(function(item){
    return item>1
})
console.log(demo);


//map 
Array.prototype.myMap=function(fn){
    const arr1=[]
    for(let i=0;i<this.length;i++){
        arr1.push(fn(this[i])) 
    }
    return arr1

}
const newArr=arr.myMap((item)=>{
    return item*2
})
console.log(newArr);
  </script>
</body>
</html>